import axios from "axios";
export const http = axios.create({
  baseURL: "/api",
});

let errorCache = new Set();
// 添加拦截器
http.interceptors.request.use(
  (res) => {
    const token = localStorage.getItem("token");
    console.log(token, "token");
    if (token) {
      res.headers.Authorization = token;
      !res.headers.Authorization && console.log("token未绑定");
    } else {
      // window.location.href = "/Login";
    }
    return res;
  },
  (err) => {
    console.log(err);
  }
);
http.interceptors.response.use(
  (res) => {
    return res;
  },
  (err) => {
    const status = err.response?.status;
    if (status && !errorCache.has(status)) {
      errorCache.add(status);
      if (status === 401) {
        console.log("登录过期");
        localStorage.removeItem("token");
      } else if (status === 403) {
        console.log("无权限");
      } else if (status === 500) {
        console.log("服务器错误");
      }
      // window.location.href = "/Login";
    }

    return Promise.reject(err);
  }
);
